package com.stylefeng.guns.modular.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Map.Entry;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import com.alibaba.fastjson.JSONObject;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

/**
 * 
 * ClassName: GetSQPlace 
 * @Description: 测试获取商圈经纬度
 * @author xiongyao
 * @date 2017年10月23日
 */
public class GetSQPlace {
	
	
	public String[] sqs={"江汉路"};
	public static String api="http://api.map.baidu.com/geocoder/v2/?address=";
	public static String city="&city=";
	public static String api2="&output=json&ak=sYDDhsxC9jCXFLb7nmgjXnkP4MZA4TKH&callback=showLocation";
	
	public static void main(String[] args) throws ClientProtocolException, IOException {
		BufferedReader bufferedReader=new BufferedReader(new FileReader("D:/Users/Administrator/Desktop/工作/17-19大数据框架/政企中心/7.24传神州泰岳(聚类市场)/7.24传神州泰岳(聚类市场)/wuhansq.txt"));
		BufferedWriter bufferedWriter=new BufferedWriter(new FileWriter("D:/Users/Administrator/Desktop/工作/17-19大数据框架/政企中心/7.24传神州泰岳(聚类市场)/7.24传神州泰岳(聚类市场)/wuhansqresult.txt"));
		String s;
		String[] sq;
		String returnJson;
		while ((s=bufferedReader.readLine())!=null) {
			sq=s.split(",");
			returnJson=CodecUtil.decodeURL(do_get(api+sq[1].trim()+city+sq[0].trim()+api2));
			returnJson=returnJson.substring(returnJson.indexOf("(")+1, returnJson.length()-1);
			returnJson=returnJson.replaceAll("\"", "");
			try {
				JsonObject returnData = new JsonParser().parse(returnJson).getAsJsonObject();
				bufferedWriter.write(StringUtil.concat(sq[0],sq[1],returnData.getAsJsonObject("result").getAsJsonObject("location").get("lng").getAsString(),returnData.getAsJsonObject("result").getAsJsonObject("location").get("lat").getAsString())+"\n");
			} catch (Exception e) {
				// TODO: handle exception
				bufferedWriter.write(StringUtil.concat(sq[0],sq[1],"not found")+"\n");
			}
		}
		bufferedReader.close();
		bufferedWriter.close();
	}
	
	public static String do_get(String url) throws ClientProtocolException, IOException {
        String body = "{}";
        DefaultHttpClient httpclient = new DefaultHttpClient();
        try {
            HttpGet httpget = new HttpGet(url);
            HttpResponse response = httpclient.execute(httpget);
            HttpEntity entity = response.getEntity();
            body = EntityUtils.toString(entity);
        } catch(Exception e){
        	
        }
        finally {
            httpclient.getConnectionManager().shutdown();
        }
        return body;
    }
}
